package com.csiec.clock.fragment.main;

import androidx.databinding.DataBindingUtil;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.csiec.clock.R;
import com.csiec.clock.databinding.NewFragmentBinding;
import com.csiec.clock.view.model.NewViewModel;

public class NewFragment extends Fragment {

    private NewViewModel newViewModel;
    private NewFragmentBinding binding;

    public static NewFragment newInstance() {
        return new NewFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        if (newViewModel == null) {
            newViewModel = new NewViewModel();
        }
        binding = DataBindingUtil.inflate(inflater, R.layout.new_fragment, container, false);
        return binding.getRoot();
    }

    @SuppressLint({"ResourceType", "SetTextI18n"})
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        newViewModel = NewViewModel.getNewViewModel();
        newViewModel.init(requireActivity(), binding);
        binding.setNewViewModel(newViewModel);
        binding.setLifecycleOwner(this);

        binding.toolbar2.setTitle("新建打卡");

        binding.switch1.setOnCheckedChangeListener((buttonView, isChecked) -> {
            newViewModel.getOpenSpace().setValue(isChecked);
            binding.editTextNumber4.setEnabled(isChecked);
            if (isChecked) {
                binding.editTextNumber4.setText("500");
            } else {
                binding.editTextNumber4.setText("");
                binding.textView5.setText("");
                binding.tvYearText2.setText("用户位置");
            }
        });
    }
}